﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.Log;

namespace DotnetWinFormApp
{
    public partial class FTestConvertEx : Form
    {
        public FTestConvertEx()
        {
            InitializeComponent();
        }

        private void FTestConvertEx_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //double? src;
            //decimal? dst;


            //src = null;
            //dst = ConvertEx.ConvertTo<decimal>(src);

            //src = 123d;
            //dst = ConvertEx.ConvertTo<double, decimal>(src);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                //var config = AssemblyConfigEx.LoadAssemblyConfig(typeof(DFLib.DFClass).Assembly);
                //int age = AssemblyConfigEx.GetValue<int>(config, "age");
                //int age11 = AssemblyConfigEx.GetValue<int>(config, null);
                //int age22 = AssemblyConfigEx.GetValue<int>(config, "age22");
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }


            try
            {
                int count = AssemblyConfigEx.GetValue<int>("count");
                int count11 = AssemblyConfigEx.GetValue<int>(string.Empty);
                int count22 = AssemblyConfigEx.GetValue<int>("count22");
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                List<Stu> list = new List<Stu>()
                {
                    new Stu(){ ID=1, Age=23, Name="yf" },
                    null,
                    new Stu(){ ID=2, Age=18, Name="lxj" },
                    new Stu(){ ID=3, Age=25, Name=null },
                    new Stu(){ ID=4, Age=21, Name="zzx" },
                    new Stu(){ ID=4, Age=24, Name="ycs" },
                    new Stu(){ ID=4, Age=26, Name="lxj" },
                };

                try
                {
                    var dic1 = DictionaryEx.ToDictionary(list, (k) => { return k.Name; }, (k, v1, v2) => { return v2; });
                    var dic2 = DictionaryEx.ToDictionary(list, (k) => { return k.Name; });
                }
                catch (Exception exi)
                {
                    Loger.Error(exi);
                }

                try
                {
                    var dic3 = DictionaryEx.ToDictionary(list, (k) => { return k.Name; }, (v) => { return v.ID; }, (k, v1, v2) => { return v2; });
                    var dic4 = DictionaryEx.ToDictionary(list, (k) => { return k.Name; }, (v) => { return v.ID; });
                }
                catch (Exception exi)
                {
                    Loger.Error(exi);
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }
    }

    public partial class Stu
    {
        public long ID { get; set; }

        [StringLength(50)]
        public string Name { get; set; }

        public int? Age { get; set; }

        public string Addr { get; set; }

        public DateTime? Bir { get; set; }

        public override string ToString()
        {
            return $"ID:{ID} Name:{Name} Age:{Age} Bir:{Bir}";
        }
    }
}
